package com.xn.app.muying.adpter;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;
import com.xn.app.muying.bean.FinishSheetBean;
import com.xn.app.muying.bean.UnfinishSheetBean;

import java.util.ArrayList;

public class FinishSheetAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context context;
    private ArrayList<FinishSheetBean> list = new ArrayList();
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;
    private Handler handler;

    public FinishSheetAdapter(Context context, Handler handler, ImageLoader loader,
                              DisplayImageOptions options) {
        super();
        this.context = context;
        this.handler = handler;
        this.imageLoader = loader;
        this.options = options;
        inflater = LayoutInflater.from(context);
    }

    public void getData(ArrayList<FinishSheetBean> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.finishsheet_item, null);
            holder = new ViewHolder();
            holder.iv = (ImageView) view.findViewById(R.id.finishsheet_item_iv);
            holder.title = (TextView) view.findViewById(R.id.finishsheet_item_title);
            holder.page = (TextView) view.findViewById(R.id.finishsheet_item_page);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (list.get(i).getPictureUrl() != null && !list.get(i).getPictureUrl().equals("")) {
            //设置图片，如果内存中有，优先从内存中读取
            imageLoader.displayImage(list.get(i).getPictureUrl(), holder.iv, options);
        } else {
            holder.iv.setImageResource(R.mipmap.load_failed);
        }
        holder.title.setText(list.get(i).getDisplayName());
        holder.page.setText(list.get(i).getReportCount() + "张");
        return view;
    }

    class ViewHolder {
        ImageView iv;
        TextView title, page;
    }
}
